home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / include / x11 / poly.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  9.7 KB  |  273 lines

  1. /* $XConsortium: poly.h,v 1.3 91/03/08 13:44:31 rws Exp $ */
  2. /************************************************************************
  3. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  4. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Digital or MIT not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15.  
  16. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ************************************************************************/
  25.  
  26. /*
  27.  *     This file contains a few macros to help track
  28.  *     the edge of a filled object.  The object is assumed
  29.  *     to be filled in scanline order, and thus the
  30.  *     algorithm used is an extension of Bresenham's line
  31.  *     drawing algorithm which assumes that y is always the
  32.  *     major axis.
  33.  *     Since these pieces of code are the same for any filled shape,
  34.  *     it is more convenient to gather the library in one
  35.  *     place, but since these pieces of code are also in
  36.  *     the inner loops of output primitives, procedure call
  37.  *     overhead is out of the question.
  38.  *     See the author for a derivation if needed.
  39.  */
  40.  
  41.  
  42. /*
  43.  *  In scan converting polygons, we want to choose those pixels
  44.  *  which are inside the polygon.  Thus, we add .5 to the starting
  45.  *  x coordinate for both left and right edges.  Now we choose the
  46.  *  first pixel which is inside the pgon for the left edge and the
  47.  *  first pixel which is outside the pgon for the right edge.
  48.  *  Draw the left pixel, but not the right.
  49.  *
  50.  *  How to add .5 to the starting x coordinate:
  51.  *      If the edge is moving to the right, then subtract dy from the
  52.  *  error term from the general form of the algorithm.
  53.  *      If the edge is moving to the left, then add dy to the error term.
  54.  *
  55.  *  The reason for the difference between edges moving to the left
  56.  *  and edges moving to the right is simple:  If an edge is moving
  57.  *  to the right, then we want the algorithm to flip immediately.
  58.  *  If it is moving to the left, then we don't want it to flip until
  59.  *  we traverse an entire pixel.
  60.  */
  61. #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
  62.     int dx;      /* local storage */ \
  63. \
  64.     /* \
  65.      *  if the edge is horizontal, then it is ignored \
  66.      *  and assumed not to be processed.  Otherwise, do this stuff. \
  67.      */ \
  68.     if ((dy) != 0) { \
  69.         xStart = (x1); \
  70.         dx = (x2) - xStart; \
  71.         if (dx < 0) { \
  72.             m = dx / (dy); \
  73.             m1 = m - 1; \
  74.             incr1 = -2 * dx + 2 * (dy) * m1; \
  75.             incr2 = -2 * dx + 2 * (dy) * m; \
  76.             d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
  77.         } else { \
  78.             m = dx / (dy); \
  79.             m1 = m + 1; \
  80.             incr1 = 2 * dx - 2 * (dy) * m1; \
  81.             incr2 = 2 * dx - 2 * (dy) * m; \
  82.             d = -2 * m * (dy) + 2 * dx; \
  83.         } \
  84.     } \
  85. }
  86.  
  87. #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
  88.     if (m1 > 0) { \
  89.         if (d > 0) { \
  90.             minval += m1; \
  91.             d += incr1; \
  92.         } \
  93.         else { \
  94.             minval += m; \
  95.             d += incr2; \
  96.         } \
  97.     } else {\
  98.         if (d >= 0) { \
  99.             minval += m1; \
  100.             d += incr1; \
  101.         } \
  102.         else { \
  103.             minval += m; \
  104.             d += incr2; \
  105.         } \
  106.     } \
  107. }
  108.  
  109.  
  110. /*
  111.  *     This structure contains all of the information needed
  112.  *     to run the bresenham algorithm.
  113.  *     The variables may be hardcoded into the declarations
  114.  *     instead of using this structure to make use of
  115.  *     register declarations.
  116.  */
  117. typedef struct {
  118.     int minor_axis;    /* minor axis        */
  119.     int d;        /* decision variable */
  120.     int m, m1;        /* slope and slope+1 */
  121.     int incr1, incr2;    /* error increments */
  122. } BRESINFO;
  123.  
  124.  
  125. #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
  126.     BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
  127.                      bres.m, bres.m1, bres.incr1, bres.incr2)
  128.  
  129. #define BRESINCRPGONSTRUCT(bres) \
  130.         BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
  131.  
  132.  
  133.  
  134. /*
  135.  *     These are the data structures needed to scan
  136.  *     convert regions.  Two different scan conversion
  137.  *     methods are available -- the even-odd method, and
  138.  *     the winding number method.
  139.  *     The even-odd rule states that a point is inside
  140.  *     the polygon if a ray drawn from that point in any
  141.  *     direction will pass through an odd number of
  142.  *     path segments.
  143.  *     By the winding number rule, a point is decided
  144.  *     to be inside the polygon if a ray drawn from that
  145.  *     point in any direction passes through a different
  146.  *     number of clockwise and counter-clockwise path
  147.  *     segments.
  148.  *
  149.  *     These data structures are adapted somewhat from
  150.  *     the algorithm in (Foley/Van Dam) for scan converting
  151.  *     polygons.
  152.  *     The basic algorithm is to start at the top (smallest y)
  153.  *     of the polygon, stepping down to the bottom of
  154.  *     the polygon by incrementing the y coordinate.  We
  155.  *     keep a list of edges which the current scanline crosses,
  156.  *     sorted by x.  This list is called the Active Edge Table (AET)
  157.  *     As we change the y-coordinate, we update each entry in 
  158.  *     in the active edge table to reflect the edges new xcoord.
  159.  *     This list must be sorted at each scanline in case
  160.  *     two edges intersect.
  161.  *     We also keep a data structure known as the Edge Table (ET),
  162.  *     which keeps track of all the edges which the current
  163.  *     scanline has not yet reached.  The ET is basically a
  164.  *     list of ScanLineList structures containing a list of
  165.  *     edges which are entered at a given scanline.  There is one
  166.  *     ScanLineList per scanline at which an edge is entered.
  167.  *     When we enter a new edge, we move it from the ET to the AET.
  168.  *
  169.  *     From the AET, we can implement the even-odd rule as in
  170.  *     (Foley/Van Dam).
  171.  *     The winding number rule is a little trickier.  We also
  172.  *     keep the EdgeTableEntries in the AET linked by the
  173.  *     nextWETE (winding EdgeTableEntry) link.  This allows
  174.  *     the edges to be linked just as before for updating
  175.  *     purposes, but only uses the edges linked by the nextWETE
  176.  *     link as edges representing spans of the polygon to
  177.  *     drawn (as with the even-odd rule).
  178.  */
  179.  
  180. /*
  181.  * for the winding number rule
  182.  */
  183. #define CLOCKWISE          1
  184. #define COUNTERCLOCKWISE  -1 
  185.  
  186. typedef struct _EdgeTableEntry {
  187.      int ymax;             /* ycoord at which we exit this edge. */
  188.      BRESINFO bres;        /* Bresenham info to run the edge     */
  189.      struct _EdgeTableEntry *next;       /* next in the list     */
  190.      struct _EdgeTableEntry *back;       /* for insertion sort   */
  191.      struct _EdgeTableEntry *nextWETE;   /* for winding num rule */
  192.      int ClockWise;        /* flag for winding number rule       */
  193. } EdgeTableEntry;
  194.  
  195.  
  196. typedef struct _ScanLineList{
  197.      int scanline;              /* the scanline represented */
  198.      EdgeTableEntry *edgelist;  /* header node              */
  199.      struct _ScanLineList *next;  /* next in the list       */
  200. } ScanLineList;
  201.  
  202.  
  203. typedef struct {
  204.      int ymax;                 /* ymax for the polygon     */
  205.      int ymin;                 /* ymin for the polygon     */
  206.      ScanLineList scanlines;   /* header node              */
  207. } EdgeTable;
  208.  
  209.  
  210. /*
  211.  * Here is a struct to help with storage allocation
  212.  * so we can allocate a big chunk at a time, and then take
  213.  * pieces from this heap when we need to.
  214.  */
  215. #define SLLSPERBLOCK 25
  216.  
  217. typedef struct _ScanLineListBlock {
  218.      ScanLineList SLLs[SLLSPERBLOCK];
  219.      struct _ScanLineListBlock *next;
  220. } ScanLineListBlock;
  221.  
  222.  
  223.  
  224. /*
  225.  *
  226.  *     a few macros for the inner loops of the fill code where
  227.  *     performance considerations don't allow a procedure call.
  228.  *
  229.  *     Evaluate the given edge at the given scanline.
  230.  *     If the edge has expired, then we leave it and fix up
  231.  *     the active edge table; otherwise, we increment the
  232.  *     x value to be ready for the next scanline.
  233.  *     The winding number rule is in effect, so we must notify
  234.  *     the caller when the edge has been removed so he
  235.  *     can reorder the Winding Active Edge Table.
  236.  */
  237. #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
  238.    if (pAET->ymax == y) {          /* leaving this edge */ \
  239.       pPrevAET->next = pAET->next; \
  240.       pAET = pPrevAET->next; \
  241.       fixWAET = 1; \
  242.       if (pAET) \
  243.          pAET->back = pPrevAET; \
  244.    } \
  245.    else { \
  246.       BRESINCRPGONSTRUCT(pAET->bres); \
  247.       pPrevAET = pAET; \
  248.       pAET = pAET->next; \
  249.    } \
  250. }
  251.  
  252.  
  253. /*
  254.  *     Evaluate the given edge at the given scanline.
  255.  *     If the edge has expired, then we leave it and fix up
  256.  *     the active edge table; otherwise, we increment the
  257.  *     x value to be ready for the next scanline.
  258.  *     The even-odd rule is in effect.
  259.  */
  260. #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
  261.    if (pAET->ymax == y) {          /* leaving this edge */ \
  262.       pPrevAET->next = pAET->next; \
  263.       pAET = pPrevAET->next; \
  264.       if (pAET) \
  265.          pAET->back = pPrevAET; \
  266.    } \
  267.    else { \
  268.       BRESINCRPGONSTRUCT(pAET->bres); \
  269.       pPrevAET = pAET; \
  270.       pAET = pAET->next; \
  271.    } \
  272. }
  273.